<template>
  <div class="createMarketPrice" @click.stop="aotuTab=false">
    <jc-button
      :btn-arr="['reload', 'save', 'remove', 'pass', 'sub', 'list']"
      :disab-arr="[isDisabled && 'save']"
      :list="'/customerBomList'"
      @save="subMarker"
      @refresh="getCustonerBom"
      @del="delCustom"
      @audit="Retrial(1)"
      @noAudit="Retrial(2)"
      @submit="subMarker(1)"
      @noSubmit="materSub(2)"
    />
    <el-tabs v-model="activeName" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.state')" prop="fmatername" label-width="110px">
            <el-tag :type="prodValue.stateColor">{{ prodValue.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billNo')" prop="fbillno" label-width="110px">
            <el-input v-model.trim="prodValue.fbillno" disabled />
          </el-form-item>
          <el-form-item :label="$t('shop.name1')" prop="fmatername" label-width="110px">
            <el-input v-model.trim="prodValue.fmatername" size="mini" :disabled="isDisabled" />
          </el-form-item>
          <el-form-item :label="$t('market.cust')" prop="fcustomerid">
            <el-autocomplete
              v-model.trim="prodValue.fcust"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch"
              size="mini"
              :disabled="isDisabled"
              @focus="queryTBdCustomerList"
              @select="clientData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="clientVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
          <el-form-item :label="$t('market.note')" prop="fnote">
            <el-input
              v-model.trim="prodValue.fnote"
              type="textarea"
              size="mini"
              :disabled="isDisabled"
            />
          </el-form-item>
        </el-form>
        <div style="margin-bottom: 5px;">
          <el-button size="mini" @click="newHang">{{ $t('btn.newHang') }}<!--新增行--></el-button>
          <el-button size="mini" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
          <el-button size="mini" @click="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
        </div>
        <jc-table
          :table-data="priceDetails"
          :table-header="tableHeader"
          :table-height="tableHeight"
          serial
          @rowClick="rowClick"
          @cellClick="cellClick"
        >
          <el-table-column :label="$t('market.custmatNo')" prop="fcustmatno" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcustmatno" size="mini" :disabled="isDisabled" />
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.custmatname')" prop="fcustmatname" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcustmatname" size="mini" :disabled="isDisabled" />
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.fmaterial')" prop="fmaterialId" align="center" width="150px">
            <template slot-scope="scope">
              <el-input
                v-model.trim="scope.row.fnumber"
                size="mini"
                :disabled="isDisabled"
                @input="getAtuo(scope.$index, scope.row.fnumber)"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleGetMateriel( scope.$index)"
                />
              </el-input>
              <div v-if="aotuTab && tableIndex === scope.$index" :class="tableIndex<7 ? 'tabPosit': 'tabPosit1'">
                <number-code :fnumber="fnumber1" @material="materielSelectRow" />
              </div>
            </template>
          </el-table-column>
          <el-table-column :label="$t('shop.fname2')" prop="fname" align="center" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('market.fmodel')" prop="fmodel" align="center" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('shop.fspecificaTion')" prop="fspecificaTion" align="center" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('market.fdescripTion')" prop="fdescription" align="center" min-width="200px" :show-overflow-tooltip="true" />
          <el-table-column :label="$t('market.barcode')" prop="fjcbarcode" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fjcbarcode" size="mini" :disabled="isDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="CBM" prop="cbm" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cbm" size="mini" :disabled="isDisabled" />
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.handle')" width="80px" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button type="danger" :disabled="isDisabled" size="mini" @click="delectSale(scope.$index)">{{ $t('shop.delete') }}</el-button>
            </template>
          </el-table-column>
        </jc-table>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="prodValue.fbillno"
          other-height="65vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--客户列表-->
    <client v-if="clientVisiblit" :msg="prodValue.fcreateOrgId" @client="clientData" />
    <!--    物料弹窗-->
    <material v-if="isMaterielDialog" @material="materielSelectRow" />
  </div>
</template>
<script>
import {
  custmatAudit,
  custmatAuSubmit,
  custmatSubmit, custmatUnAudit, deleteCustmat,
  queryCustmatmappingEntry,
  updateCustmat
} from '@/api/basicManagement/customerBom'
import { toMxBmina } from '@/components/ToMxamineState'
import jumpMateriel from '@/components/JumpMateriel'
import searData from '@/components/Search/mixin'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'

export default {
  name: 'CreateMarketPrice',
  components: {
    client: () => import('@/views/market/marketManage/createMarkerOrder/components/client'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    jcMarker,
    numberCode: () => import('@/components/NumberCode'),
    jcButton
  },
  mixins: [jumpMateriel, searData, autocomplete],

  data() {
    return {
      activeName: 'first',
      clientVisiblit: false, // 客户弹窗
      isMaterielDialog: false, // 物料弹窗
      aotuTab: false,
      fnumber1: '',
      materialId: '', // 物料ID
      otherUrlObject: {},
      // 点击行的序号
      tableIndex: 0,
      priceDetails: [],
      tableHeader: [],
      isDisabled: false,
      tableHeight: '68vh',
      prodValue: {}
    }
  },
  created() {
    this.getCustonerBom()
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 280px)' : '67vh'
  },
  methods: {
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.tableIndex = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber1 = fnumber
      }, 1000)
    },
    // 获取数据
    getCustonerBom() {
      const fid = this.$route.params.id
      queryCustmatmappingEntry({ fid }).then(res => {
        if (res.code === 0) {
          this.prodValue = toMxBmina(res.data)
          this.otherUrlObject = res.data
          this.priceDetails = res.data.details
          this.isDisabled = this.prodValue.fdocumentstatus === this.$t('market.state1') || this.prodValue.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    subMarker(ev) {
      this.prodValue.details = this.priceDetails
      const DATA = this.prodValue
      updateCustmat(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.materSub(1) : this.getCustonerBom()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 打开物料弹窗
    handleGetMateriel(index) {
      this.tableIndex = index
      this.isMaterielDialog = true
    },
    // 物料弹窗选中
    async materielSelectRow(item) {
      const items = this.priceDetails[this.tableIndex]
      items.fmaterialId = item.fmaterialId
      items.fnumber = item.fnumber
      items.fname = item.fname
      items.fmodel = item.fmodel
      items.fspecificaTion = item.fspecificaTion
      items.fdescription = item.fdescription
      this.isMaterielDialog = false
    },
    // 获取客户数据(子传父)
    clientData(item) {
      if (item.fcustId) {
        this.prodValue.fcustomerid = item.fcustId
        this.prodValue.fcust = item.fname
      }
      this.clientVisiblit = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.prodValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.getCustonerBom()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async materSub(ev) {
      const api = ev === 1 ? custmatSubmit : custmatAuSubmit
      this.toExamineFun(api)
    },
    // 审核
    async Retrial(ev) {
      const api = ev === 1 ? custmatAudit : custmatUnAudit
      this.toExamineFun(api)
    },
    // 删除客户
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const DATA = { Numbers: [this.prodValue.fbillno] }
        deleteCustmat(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/customerBomList' })
        })
      })
    },
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    },
    // 删除明细空行
    delectSale(index) {
      this.priceDetails.splice(index, 1)
    },
    // 新增行
    newHang() {
      this.priceDetails.push(
        {
          fentryid: 0,
          fmaterialId: '', // 物料编码ID
          fnumber: '', // 物料编码
          fmodel: '',
          fspecificaTion: '',
          fjcbarcode: '',
          fcustmatname: '',
          fcustmatno: ''
        }
      )
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 删除行
    delHang() {
      this.priceDetails.splice(this.indexSelf, 1)
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.priceDetails[this.subscript][this.property]
      this.priceDetails.map((res, index) => {
        if (index >= this.subscript) {
          res[this.property] = item
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.createMarketPrice {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .tabPosit {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: 3.5vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 99999!important;
  }
  .tabPosit1 {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: -25.69vh;
    left:0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 9999;
  }
  .el-table {
    margin-top: 5px;

    &::v-deep td {
      padding: 8px 0;
    }

    &::v-deep thead.is-group th {
      padding: 3px 0;

      .el-input__inner {
        background: #e6ebfc;
        color: #909399;
        font-size: 12px;
        font-weight: 550;
        border: none;
      }
    }
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      width: 275px;
      margin-bottom: 0;
    }
  }
}

.el-input__icon {
  cursor: pointer;
}

.materiel-form {
  position: relative;
  width: 100%;
  bottom: 5px;
  .btn {
    transform: translateY(18%);
    margin-left: 450px !important;
    z-index: 999;
  }
  .btn1 {
    margin-left: 15px !important;
  }
}
</style>
